﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Photon4
{
    public partial class FormViewer : Form
    {
        FormBrowser browser;

        private void _switchToBrowser()
        {
            this.Visible = false;
            browser.Visible = true;     
        }

        public FormViewer(FormBrowser br)
        {
            InitializeComponent();

            browser = br;
        }

        public void UpdateUI()
        {
            bool hasFileToProcess = browser.fileProcessor.GetCurrentFile() != "";
            copyToToolStripMenuItem.Enabled = hasFileToProcess;
            moveToToolStripMenuItem.Enabled = hasFileToProcess;
            copyImageToolStripMenuItem.Enabled = hasFileToProcess;
            deleteToolStripMenuItem.Enabled = hasFileToProcess;
            reToolStripMenuItem.Enabled = hasFileToProcess;
            describeToolStripMenuItem.Enabled = hasFileToProcess;
            closeImageToolStripMenuItem.Enabled = hasFileToProcess;
            fullScreenToolStripMenuItem.Enabled = hasFileToProcess;

            toolbarToolStripMenuItem.Checked = this.toolStrip1.Visible;
            statusBarToolStripMenuItem.Checked = this.statusStrip1.Visible;
            string strStatusText = "";
            strStatusText = browser.fileProcessor.CurrentIndex().ToString();
            strStatusText += "/";
            strStatusText += browser.fileProcessor.FileCount().ToString();
            toolStripStatusLabel1.Text = strStatusText;
            toolStripStatusLabel2.Text = System.IO.Path.GetFileName(browser.fileProcessor.GetCurrentFile());
            toolStripStatusLabel3.Text = pictureBox1.Zoom.ToString() + "% ";
            toolStripStatusLabel4.Text = pictureBox1.Description;
        }

        public void OpenImage(string strFileName)
        {
            pictureBox1.LoadImage(strFileName);
        }

        private void FormViewer_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void browserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._switchToBrowser();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            browser.ShowAboutBox();
        }

        private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void browserToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this._switchToBrowser();
        }

        private void describeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            browser.fileProcessor.DescribeFile(pictureBox1);
        }

        private void closeImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //pictureBox1.Image = null;
            //pictureBox1.Refresh();
            browser.fileProcessor.SetCurrentFile("");
        }

        private void toolbarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.toolStrip1.Visible = !toolStrip1.Visible;
        }

        private void statusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.statusStrip1.Visible = !this.statusStrip1.Visible;
        }

        private void copyToToolStripMenuItem_Click(object sender, EventArgs e)
        {
            browser.fileProcessor.CopyToFile();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            browser.fileProcessor.DeleteFile();
            if (browser.fileProcessor.GetCurrentFile() == "")
            {
                //pictureBox1.Image = null;
                pictureBox1.Invalidate();
            }
            else
            {
                pictureBox1.LoadImage(browser.fileProcessor.GetCurrentFile());
            }
        }

        private void reToolStripMenuItem_Click(object sender, EventArgs e)
        {
            browser.fileProcessor.RenameFile();
        }

        private void moveToToolStripMenuItem_Click(object sender, EventArgs e)
        {
            browser.fileProcessor.MoveToFile();
            if (browser.fileProcessor.GetCurrentFile() != "")
                pictureBox1.LoadImage(browser.fileProcessor.GetCurrentFile());
            else
            {
                //pictureBox1.Image = null;
                //pictureBox1.Invalidate();
            }
        }

        private void nextMediaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.LoadImage(browser.fileProcessor.GetNextFile());
        }

        private void prevMediaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.LoadImage(browser.fileProcessor.GetPrevFile());
        }

        private void viewerContextMenuStrip_Opening(object sender, CancelEventArgs e)
        {

        }

        private void copyToToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            browser.fileProcessor.CopyToFile();
        }

        private void moveToToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            browser.fileProcessor.MoveToFile();
            if (browser.fileProcessor.GetCurrentFile() != "")
                pictureBox1.LoadImage(browser.fileProcessor.GetCurrentFile());
            else
            {
                //pictureBox1.Image = null;
                //pictureBox1.Invalidate();
            }
        }

        private void deleteToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            browser.fileProcessor.DeleteFile();
            if (browser.fileProcessor.GetCurrentFile() == "")
            {
                //pictureBox1.Image = null;
                //pictureBox1.Invalidate();
            }
            else
            {
                pictureBox1.LoadImage(browser.fileProcessor.GetCurrentFile());
            }
        }

        private void describeToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            browser.fileProcessor.DescribeFile(pictureBox1);
        }

        private void renameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            browser.fileProcessor.RenameFile();
        }

        private void nextFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.LoadImage(browser.fileProcessor.GetNextFile());
        }

        private void prevFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.LoadImage(browser.fileProcessor.GetPrevFile());
        }

        private void openImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.LoadImage(openFileDialog1.FileName);
                browser.fileProcessor.SetCurrentFile(openFileDialog1.FileName);
                pictureBox1.Tool = new PhotonControls.FZoomToRectTool(pictureBox1);
            }
        }

        private void actualSizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.ActualSize();
        }

        private void bestFitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.BestFit();
        }

        private void zoomImToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.ZoomIn();
        }

        private void zoomOutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.ZoomOut();
        }
        private void actualSizeToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            pictureBox1.ActualSize();
        }

        private void bestFitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            pictureBox1.BestFit();
        }

        private void fitWidthToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.FitWidth();
        }

        private void fitHeightToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.FitHeight();
        }

        private void zoomPaneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.Tool = null;
        }

        private void zoomToRectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.Tool = new PhotonControls.FZoomToRectTool(pictureBox1);
        }


    }
}
